package com.tripshare.logic.common;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;

import java.text.SimpleDateFormat;

/**
 */
public class Result<T> {

    public static final Result SUCCESS = createSuccessResult();
    public static final Result ERROR = createErrorResult();
    public static final Result BAD_REQUEST = createErrorResult(HttpStatus.BAD_REQUEST);
    public static final Result NOT_FOUND = createErrorResult(HttpStatus.NOT_FOUND);

    public static <T> Result<T> createSuccessResult(T data) {
        Result<T> result = new Result<>();
        result.setCode(0);
        result.setData(data);
        result.setErrMsg("success");
        return result;
    }

    public static <T> Result<T> createSuccessResult() {
        return createSuccessResult((T) "success");
    }

    public static <T> Result<T> createSuccessResult(HttpStatus httpStatus, T data) {
        Result<T> result = new Result<>();
        result.setCode(httpStatus.value());
        result.setData(data);
        result.setErrMsg(httpStatus.getReasonPhrase());
        return result;
    }

    public static <T> Result<T> createSuccessResult(HttpStatus httpStatus) {
        Result<T> result = new Result<>();
        result.setCode(httpStatus.value());
        result.setData((T)httpStatus.getReasonPhrase());
        result.setErrMsg("success");
        return result;
    }

    public static <T> Result<T> createErrorResult() {
        Result<T> result = new Result<>();
        result.setCode(500);
        result.setData(null);
        result.setErrMsg("ERROR");
        return result;
    }

    public static <T> Result<T> createErrorResult(T data) {
        Result<T> result = new Result<>();
        result.setCode(500);
        result.setData(data);
        result.setErrMsg("ERROR");
        return result;
    }

    public static <T> Result<T> createErrorResult(int returnCode, T data) {
        Result<T> result = new Result<>();
        result.setCode(returnCode);
        result.setData(data);
        result.setErrMsg("ERROR");
        return result;
    }


    public static <T> Result<T> createErrorResult(HttpStatus httpStatus) {
        Result<T> result = new Result<>();
        result.setCode(httpStatus.value());
        result.setData(null);
        result.setErrMsg(httpStatus.getReasonPhrase());
        return result;
    }

    public static <T> Result<T> createErrorResult(int returnCode,String returnMsg) {
        Result<T> result = new Result<>();
        result.setCode(returnCode);
        result.setData(null);
        result.setErrMsg(returnMsg);
        return result;
    }



    private static ObjectMapper objectMapper = new ObjectMapper();
    static {
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
    private T data;

    private int code;

    private String errMsg;

    public int getCode() {
        return code;
    }


    public T getData(Class<T> clazz) {
        if (data == null) {
            return null;
        }
        return objectMapper.convertValue(data, clazz);
    }

    public Result() {
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }


    @Override
    public String toString() {
        return "Result{" +
                "data=" + data +
                ", code=" + code +
                ", errMsg='" + errMsg + '\'' +
                '}';
    }
}
